package com.upbos.upm.dept;

import com.upbos.core.ret.RetCode;
import com.upbos.core.ret.RetData;
import com.upbos.core.util.StringPool;
import com.upbos.sso.UserContext;
import com.upbos.upm.entity.Dept;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <b>Application name：</b> DeptController.java <br>
 * <b>Application describing：部门管理Service </b> <br>
 * <b>Copyright：</b> Copyright &copy; 2019 upbos.com 版权所有。<br>
 * <b>Company：</b> upbos.com <br>
 * <b>@Date：</b> 2019年11月08日 13:59 <br>
 * <b>@author：</b> <a href="mailto:Jason@miyzh.com"> Jason </a> <br>
 * <b>@version：</b>V5.1.0 <br>
 */
@Service
public class DeptService {
    @Resource
    private DeptDao deptDao;

    /**
     * 查询下级部门
     *
     * @param parentId 上级部门Id
     * @return 下级部门列表
     */
    public List<Dept> listDeptByParentId(String parentId) {
        return deptDao.listDeptByParentId(parentId);
    }

    /**
     * 根据部门名称模糊查询
     *
     * @param name 部门名称
     * @return 部门列表
     */
    public List<Dept> listDept(String orgId, String name) {
        return deptDao.listDept(orgId, name);
    }

    @Transactional(rollbackFor = Exception.class)
    public Dept saveDept(Dept dept, UserContext context) {

        if (dept.getId() == null) {
            // 插入部门
            if (dept.getOrgId() == null || "".equals(dept.getOrgId())) {
                dept.setOrgId(context.getSsoUser().getOrgId());
            }
            insertDept(dept);
        } else {
            // 更新部门
            deptDao.updateDept(dept);
        }
        return dept;
    }

    /**
     * 插入部门
     *
     * @param dept
     */
    public Dept insertDept(Dept dept) {
        // 获得父节点下排序号最大的节点
        Dept maxNode = deptDao.getMaxDept(dept.getParentId());
        if (maxNode == null) {
            dept.setIdx(1);
            if (dept.getParentId() != null) {
                deptDao.updateDeptLeaf(dept.getParentId(), false);
            }
        } else {
            dept.setIdx(maxNode.getIdx().intValue() + 1);
        }

        // 获得上级节点的层级代码
        String parentNodeCascade = deptDao.getDeptCascade(dept.getParentId());

        deptDao.insertDept(dept);
        dept.setCascade(parentNodeCascade + (StringPool.EMPTY.equals(parentNodeCascade) ? StringPool.EMPTY : StringPool.DOT) + dept.getId());
        deptDao.updateDept(dept);
        return dept;
    }

    @Transactional(rollbackFor = Exception.class)
    public RetData deleteDept(Dept dept) {
        RetData retData = new RetData(RetCode.SUCCESS);
        if (deptDao.getDeptUserCount(dept) > 0  ) {
            retData.setCode(RetCode.FAIL,"该部门下还有用户，请先行删除！");
            return retData;
        }

        deptDao.deleteDept(dept);
        int childrenDeptCount = deptDao.getDeptNumberByParentId(dept.getParentId());
        if (childrenDeptCount == 0 && dept.getParentId() != null) {
            deptDao.updateDeptLeaf(dept.getParentId(), true);
        }
        return retData;
    }


}
